package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.VWashWork;
import com.tianxin.workbao.workBao.bean.WashCoaltype;
import com.tianxin.workbao.workBao.bean.WashWork;
import com.tianxin.workbao.workBao.dataProvide.WashWorkProvide;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.AlertUtils;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.view.ExpandView;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by hacker on 2017/7/25.
 */

public class WashWorkAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<VWashWork> vWashWorkList;
    private Context mContext;

    public WashWorkAdapter(List<VWashWork> list, Context context) {
        this.vWashWorkList = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (vWashWorkList.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.wash_work_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(vWashWorkList.get(position));
            viewHolder.setViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        if (vWashWorkList.size() > 0) {
            return vWashWorkList.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 1;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        ExpandView expandView;
        TextView inWeight;
        TextView outWeight;
        TextView differWeight;
        TextView addTime;
        TextView infoTV;
        TextView useElectricTV;
        TextView useMedicineTV;
        TextView useOilTV;
        TextView inCoalTV;
        TextView outCoalTV;
        Button deleteBtn;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.arrow);
            expandView = (ExpandView) itemView.findViewById(R.id.expandView);
            inWeight = (TextView) itemView.findViewById(R.id.in_weight);
            outWeight = (TextView) itemView.findViewById(R.id.out_weight);
            differWeight = (TextView) itemView.findViewById(R.id.differ_weight);
            addTime = (TextView) itemView.findViewById(R.id.add_time);
            infoTV = (TextView) itemView.findViewById(R.id.info);
            useElectricTV = (TextView) itemView.findViewById(R.id.use_electric);
            useMedicineTV = (TextView) itemView.findViewById(R.id.use_medicine);
            useOilTV = (TextView) itemView.findViewById(R.id.use_oil);
            inCoalTV = (TextView) itemView.findViewById(R.id.in_coal);
            outCoalTV = (TextView) itemView.findViewById(R.id.out_coal);
            deleteBtn = (Button) itemView.findViewById(R.id.delete_btn);
        }

        public void BindViewData(VWashWork vWashWork) {
            WashWork work = vWashWork.getWashWork();
            DecimalFormat df = new DecimalFormat("0.00");
            inWeight.setText(work.getIntotal() + "");
            outWeight.setText(work.getOuttotal() + "");
            differWeight.setText(df.format(work.getOuttotal() - work.getIntotal()));
            addTime.setText(TimeUtils.parseDateTime(work.getAddtime()));
            useElectricTV.setText(work.getUseelectric() + "");
            useMedicineTV.setText(work.getUsemedecine() + "");
            useOilTV.setText(work.getUseoil() + "");
            String info = "";
            info = TimeUtils.parseTime(work.getStarttime()) + " 开机到"
                    + TimeUtils.parseTime(work.getEndtime()) + " 关机，共 " +
                    work.getUsetime() + " 小时，" +
                    work.getWorkers() + " 人上班 备注：" + work.getRemark() +
                    " 录入人：" + work.getAddusername();
            infoTV.setText(info);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (sdf.format(work.getAddtime()).equals(sdf.format(new Date()))) {
                deleteBtn.setVisibility(View.VISIBLE);
            } else {
                deleteBtn.setVisibility(View.GONE);
            }
            List<WashCoaltype> inWashCoaltypes = vWashWork.getInWashCoaltypes();
            outCoalTV.setText("");
            inCoalTV.setText("");
            for (int i = 0; i < inWashCoaltypes.size(); i++) {
                if (inCoalTV.getText().length() < 1) {
                    inCoalTV.setText(inWashCoaltypes.get(i).getCoaltype() + "   "
                            + inWashCoaltypes.get(i).getTonnage());
                } else {
                    inCoalTV.setText(inCoalTV.getText() + "\n" + inWashCoaltypes.get(i).getCoaltype() + "   "
                            + inWashCoaltypes.get(i).getTonnage());
                }
            }
            List<WashCoaltype> outWashCoaltypes = vWashWork.getOutWashCoaltypes();
            for (int i = 0; i < outWashCoaltypes.size(); i++) {
                if (outCoalTV.getText().length() < 1) {
                    outCoalTV.setText(outWashCoaltypes.get(i).getCoaltype() + "   "
                            + outWashCoaltypes.get(i).getTonnage());
                } else {
                    outCoalTV.setText(outCoalTV.getText() + "\n" + outWashCoaltypes.get(i).getCoaltype() + "   "
                            + outWashCoaltypes.get(i).getTonnage());
                }
            }
        }

        public void setViewListener(final int position) {

            final View.OnClickListener listener = new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (view.getId()) {
                        case R.id.arrow:
                            if (expandView.isExpand()) {
                                expandView.collapse();
                                imageView.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp);
                            } else {
                                expandView.expand();
                                imageView.setImageResource(R.drawable.ic_arrow_drop_up_black_24dp);
                            }
                            break;
                        case R.id.delete_btn:
                            WashWorkProvide provide;
                            final AlertUtils alert = new AlertUtils(mContext);
                            provide = new WashWorkProvide(mContext, new UpdateListViewListener() {
                                @Override
                                public void onRefreshData(Object result) {

                                }

                                @Override
                                public void onLoadMoreData(Object result) {

                                }

                                @Override
                                public void showMessage(String msg) {
                                    if (msg == "ok") {
                                        expandView.collapse();
                                        vWashWorkList.remove(position);
                                        notifyItemRemoved(position);
                                    } else if (msg.contains("failure")) {
                                        Toast.makeText(mContext, "删除失败！\n " + msg.substring(msg.indexOf("failure") + 1, msg.length()), Toast.LENGTH_LONG).show();
                                    } else {
                                        Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();
                                    }
                                }

                                @Override
                                public void showProgress() {
                                    alert.showDialog("", 0);
                                }

                                @Override
                                public void hideProgress() {
                                    alert.dismiss();
                                }
                            });
                            provide.delete((int) vWashWorkList.get(position).getWashWork().getId());
                            break;
                    }
                }
            };
            imageView.setOnClickListener(listener);
            deleteBtn.setOnClickListener(listener);
        }
    }
}
